import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
// import Register from '../views/Register.vue'
// import Forget from '../views/Forget.vue'

import Main from '../views/subs/Main.vue'
import Order from '../views/subs/Order.vue'
import User from '../views/subs/User.vue'
import Cashier from '../views/subs/cashier.vue'
import Sale from '../views/subs/Sale.vue'
import Update from '../views/subs/update.vue'
import Administrator from '../views/subs/Administrator.vue'
import Role from '../views/subs/Role.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta:{
      is:true,
      extraName:"首页"
    },
    children: [
      {
        path: '/',
        name: 'Main',
        component: Main,
        meta:{
          is:true,
          extraName:"首页"
        }
      },
      {
        path: 'user',
        name: 'User',
        component: User,
        meta:{
          is:true,
          extraName:"用户管理"
        }
      },
      {
        path: 'order',
        name: 'Order',
        component: Order,
        meta:{
          is:true,
          extraName:"订单管理"
        }

      },
      {
        path: 'cashier',
        name: 'Cashier',
        component: Cashier,
        meta:{
          is:true,
          extraName:"收银订单"
        }
      },
      {
        path: 'sale',
        name: 'Sale',
        component: Sale,
        meta:{
          is:true,
          extraName:"售后订单"
        }
      },
      {
        path: 'update',
        name: 'Update',
        component: Update,
        meta:{
          is:true,
          extraName:"编辑订单"
        }
      },
      {
        path: 'administrator',
        name: 'Administrator',
        component: Administrator,
        meta:{
          is:true,
          extraName:"管理员列表"
        }
      },
      {
        path: 'role',
        name: 'Role',
        component: Role,
        meta:{
          is:true,
          extraName:"角色管理"
        }
      },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: ()=>import('../views/Register.vue')
  },
  {
    path: '/forget',
    name: 'Forget',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: ()=>import('../views/Forget.vue')
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  if (to.path.indexOf('/home')!=-1) {
    if (localStorage.token && localStorage.userInfo) {
      next()
    }else{
      next('/login')
    }
  }else{
    next()
  }

})
export default router
